﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace T.FAS.Runtime.DataAccess.CDOEngine
{
    public class SelectSqlStaForMySQL : SelectSqlStatement
    {
        #region 构造函数

        /// <summary>
        /// 构造函数
        /// </summary>
        public SelectSqlStaForMySQL()
            : base() { }

        #endregion

        private DBCustomTools dBCustomTools = new DBCustomTools(DBCustomTools.DB_MySQL);
        #region 方法

        /// <summary>
        /// 转换成SQL语句
        /// </summary>
        /// <returns>Select SQL语句</returns>
        public override string ToSQL()
        {
            if (this.PageIndex <= 0 && this.PageCount <= 0)
            {
                return base.ToSQL();
            }
            else
            {
                var startIndex = (this.PageIndex - 1) * this.PageCount;
                string select = string.Format("SELECT * FROM ({0}) A Limit {1},{2};", base.ToSQL(), startIndex, this.PageCount);
                var stringBuilder = new StringBuilder();
                stringBuilder.Append(select);
                stringBuilder.Append(GetPagingSQL());
                return stringBuilder.ToString();
            }
        }

        /// <summary>
        /// 获取分页总条数方法
        /// </summary>
        /// <returns>分页总条数SQL</returns>
        private string GetPagingSQL()
        {
            var stringBuilder = new StringBuilder();
            var sqlFrom = string.Empty;

            if (string.IsNullOrWhiteSpace(sqlFrom))
            {
                sqlFrom = string.Format(" SELECT count(*) as _totalCount FROM {0}", From.ToSQL());
            }
            stringBuilder.Append(sqlFrom);


            foreach (SqlElement element in MainFromItem.ChildCollection)
            {
                string joinItemSQL = string.Empty;
                if (element is InnerJoinItem innerJoinItem)
                {
                    if (innerJoinItem.IsExtendItem)
                    {
                        joinItemSQL = innerJoinItem.ToSQLEx();
                    }
                    else
                    {
                        joinItemSQL = innerJoinItem.ToSQL();
                    }
                }
                else if (element is LeftJoinItem leftJoinItem)
                {
                    joinItemSQL = leftJoinItem.ToSQL();
                }
                joinItemSQL = dBCustomTools.Format(joinItemSQL);
                stringBuilder.Append(joinItemSQL);
            }

            var joinCondition = JoinCondition.ToSQL();
            if (!string.IsNullOrWhiteSpace(joinCondition))
            {
                stringBuilder.AppendFormat("WHERE {0} ", joinCondition);
            }

            var filterCondition = FilterCondition.ToSQL();
            if (!string.IsNullOrWhiteSpace(filterCondition))
            {
                if (string.IsNullOrWhiteSpace(joinCondition))
                {
                    stringBuilder.AppendFormat(" WHERE {0} ", filterCondition);
                }
                else
                {
                    stringBuilder.AppendFormat(" AND {0} ", filterCondition);
                }
            }
            if (OrderByCondition != null && OrderByCondition.ChildCollection.Count > 0)
            {
                var orderbyCondition = OrderByCondition.ToSQL();
                if (!string.IsNullOrWhiteSpace(orderbyCondition))
                {
                    stringBuilder.AppendFormat(" {0} ", orderbyCondition);
                }
            }
            return stringBuilder.ToString();
        }

        #endregion
    }
}
